﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using Sharp.DataTransferObject;
using Sharp.Infrastructure.Communication;
using Sharp.Platform.Mvc;
using Sharp.ServiceContracts;
using PartialViewResult = Ext.Net.MVC.PartialViewResult;

#endregion

namespace Sharp.Web.Controllers
{
    public class PermissionController : Controller
    {
        // GET: Manage/Permission
        public ActionResult ListForUser(string userName)
        {
            List<PermissionDTO> userPermissions;
            using (var proxy = new ServiceProxy<IUserService>())
            {
                userPermissions = proxy.Channel.GetPermissionByUserName(userName).ToList();
            }

            var pv = new PartialViewResult
            {
                ViewName = "TreeList",
                Model = Data(userPermissions) //(Node)null 
            };
            //pv.Model = new Node()
            //{
            //    Text = ".",
            //    NodeID = "Root",
            //    Expanded = true,
            //    AutoDataBind = false,
            //    Checked = false
            //};

            return pv;
        }

        public ActionResult ListForUserGroup(Guid? userGroupId)
        {
            IEnumerable<PermissionDTO> permissions;
            using (var proxy = new ServiceProxy<IUserService>())
            {
                permissions = proxy.Channel.GetPermissionByGroupId(userGroupId.Value);
            }

            return new PartialViewResult
            {
                ViewName = "TreeList",
                Model = DataGroup(permissions)
            };
        }

        public ActionResult Save(string owner, Guid? ownerId)
        {
            Guid[] ids = (HttpContext.Request["ids"]).FromJson<Guid[]>();

            if (ownerId == null)
            {
                throw new ArgumentNullException(nameof(ownerId));
            }
            using (var proxy = new ServiceProxy<IUserService>())
            {
                proxy.Channel.SavePermission(ids, owner, ownerId.Value);
            }

            return this.FormPanel();
        }

        private Node Data(IEnumerable<PermissionDTO> userPermissions)
        {
            var builder = new TreeBuilder<PermissionDTO>
            {
                ChangeData = (p, treeNode) =>
                {
                    treeNode.Text = p.Title;
                    //treeNode.Href = p.Route;
                    treeNode.NodeID = p.Id.ToString();
                    treeNode.Checked = userPermissions.Any(x => x.Id == p.Id);
                    treeNode.Expanded = true;
                    treeNode.AutoDataBind = false;
                }
            };

            var rootNode = new Node()
            {
                Text = ".",
                NodeID = "Root",
                Expanded = true,
                AutoDataBind = false,
                Checked = false
            };

            IList<PermissionDTO> list;
            using (var proxy = new ServiceProxy<IUserService>())
            {
                list = proxy.Channel.AllPermissions().Where(x => x.SystemCode == "Platform").ToList();

            }
            builder.Build(list, rootNode, null);

            //for (int i = 1; i < 6; i++)
            //{
            //    Node asyncNode = new Node();
            //    asyncNode.Text = "test" + i;
            //    asyncNode.NodeID = "test" + i;
            //    asyncNode.Expanded = true;
            //    asyncNode.AutoDataBind = false;
            //    asyncNode.Checked = false;
            //    rootNode.Children.Add(asyncNode);
            //}

            return rootNode;
        }

        private Node DataGroup(IEnumerable<PermissionDTO> userGroupPermissions)
        {
            var builder = new TreeBuilder<PermissionDTO>
            {
                ChangeData = (p, treeNode) =>
                {
                    treeNode.Text = p.Title;
                    //treeNode.Href = p.Route;
                    treeNode.NodeID = p.Id.ToString();
                    treeNode.Checked = userGroupPermissions.Any(x => x.Id == p.Id);
                    treeNode.Expanded = true;
                }
            };

            var rootNode = new Node()
            {
                Text = ".",
                NodeID = "Root",
                Expanded = true,
                AutoDataBind = false,
                Checked = false
            };
            using (var proxy = new ServiceProxy<IUserService>())
            {
                var list = proxy.Channel.AllPermissions().Where(x => x.SystemCode == "Platform");


                builder.Build(list, rootNode, null);
            }

            return rootNode;
        }

        public ActionResult Scan()
        {
            var scan = new PermissionScan();
            var list = scan.GetAllActionByAssembly();
            using (var proxy = new ServiceProxy<IUserService>())
            {
                proxy.Channel.UpdatePermission(list);
            }

            return null;
        }
    }
}